<template>
  <!-- <div class="qiye-container"> -->
  <el-card>
    <!-- 表单填写后搜索区域 -->
    <el-form>
      <el-row type="flex" justify="space-between" style="margin-bottom: -10px">
        <el-form-item
          label="标签名称"
          label-width="70px"
          style="font-weight: 700"
        >
          <el-input
            v-model="queryData.tags"
            size="small"
            placeholder="请输入"
          ></el-input>
        </el-form-item>
        <el-form-item label="城市" label-width="70px" style="font-weight: 700">
          <el-select
            size="small"
            v-model.trim="queryData.province"
            placeholder="请选择"
            @change="changeFn"
          >
            <el-option
              v-for="(item, index) in provincesList"
              :key="index"
              :label="item"
              :value="item"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="地区" label-width="70px" style="font-weight: 700">
          <el-select size="small" v-model="queryData.city" placeholder="请选择">
            <el-option
              v-for="(item, index) in citysList"
              :key="index"
              :label="item"
              :value="item"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          label="企业简称"
          label-width="70px"
          style="font-weight: 700"
        >
          <el-input
            v-model="queryData.shortName"
            size="small"
            placeholder="请输入"
          ></el-input>
        </el-form-item>
      </el-row>
      <el-row type="flex" justify="space-between">
        <el-form-item label="状态" label-width="70px" style="font-weight: 700">
          <el-select
            v-model="queryData.state"
            size="small"
            placeholder="请选择"
            style="margin-right: 95px"
          >
            <el-option label="启用" value="1"> </el-option>
            <el-option label="禁用" value="0"> </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button size="small" @click="resetSearch">清除</el-button>
          <el-button
            type="primary"
            size="small"
            @click="initData"
            style="margin-right: 595px"
            >搜索</el-button
          >
        </el-form-item>
        <!-- 新增用户按钮 -->
        <div style="flex: 1"></div>
        <el-form-item>
          <el-button
            type="success"
            icon="el-icon-edit"
            size="small"
            @click="showDialog = true"
            >新增用户</el-button
          >
        </el-form-item>
      </el-row>
    </el-form>
    <page-tools :text="`共${total}条记录`" />
    <!-- 表格区域 -->
    <el-table border :data="list" style="margin-bottom: 10px">
      <el-table-column label="序号" prop="id"></el-table-column>
      <el-table-column label="企业编号" prop="number"></el-table-column>
      <el-table-column label="企业简称" prop="shortName"></el-table-column>
      <el-table-column label="标签" prop="tags"></el-table-column>
      <el-table-column label="创建者" prop="creatorID"></el-table-column>
      <el-table-column label="创建日期" prop="addDate">
        <template slot-scope="{ row }">
          {{ row.addDate | changeDate }}
        </template>
      </el-table-column>
      <el-table-column label="备注" prop="remarks"> </el-table-column>
      <el-table-column label="状态" prop="state">
        <template slot-scope="scope">
          <div>{{ scope.row.state * 1 === 1 ? "启用" : "禁用" }}</div>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="180px">
        <template slot-scope="{ row }">
          <!-- 修改按钮 -->
          <el-button
            type="primary"
            circle
            plain
            size="small"
            icon="el-icon-edit"
            @click="editHandler(row)"
          ></el-button>
          <!-- 启用/禁用按钮 -->
          <el-tooltip
            :content="row.state * 1 === 0 ? '启用' : '禁用'"
            placement="top"
          >
            <el-button
              :type="row.state * 1 === 0 ? 'success' : 'warning'"
              circle
              plain
              size="small"
              :icon="row.state * 1 === 0 ? 'el-icon-check' : 'el-icon-close'"
              @click="editState(row)"
            ></el-button>
          </el-tooltip>
          <!-- 删除按钮 -->
          <el-button
            type="danger"
            circle
            plain
            size="small"
            icon="el-icon-delete"
            @click="delHandler(row.id)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页区域 -->
    <el-pagination
      style="float: right; margin-bottom: 10px"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryData.page"
      :page-sizes="[10, 8, 6]"
      :page-size="queryData.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
    <!-- 新增/修改弹框区域 -->
    <el-dialog
      :title="this.formData.id ? '编辑用户' : '新增用户'"
      :visible.sync="showDialog"
      @close="close"
    >
      <el-form
        v-if="showDialog"
        style="border-radius: 5px"
        label-width="120px"
        :model="formData"
        :rules="formRules"
        ref="form"
      >
        <el-form-item
          prop="shortName"
          label="企业名称"
          style="font-weight: 700"
        >
          <el-input
            size="small"
            v-model="formData.shortName"
            placeholder=""
          ></el-input>
        </el-form-item>
        <el-form-item prop="company" label="所属公司" style="font-weight: 700">
          <el-input
            size="small"
            v-model="formData.company"
            placeholder=""
          ></el-input>
          <div style="font-weight: 400">
            https://www.tianyancha.com （在此可查询所属公司全称及简称）
          </div>
        </el-form-item>
        <el-row type="flex" justify="start">
          <el-form-item prop="province" label="城市" style="font-weight: 700">
            <el-select
              size="small"
              v-model.trim="formData.province"
              placeholder="请选择"
              style="margin-right: 80px"
              @change="changeCn"
            >
              <el-option
                v-for="(item, index) in provincesList"
                :key="index"
                :label="item"
                :value="item"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item prop="city" label="地区" style="font-weight: 700">
            <el-select
              size="small"
              v-model="formData.city"
              placeholder="请选择"
            >
              <el-option
                v-for="(item, index) in citysList"
                :key="index"
                :label="item"
                :value="item"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-row>
        <el-form-item
          prop="tags"
          label="方向(企业标签)"
          style="font-weight: 700"
        >
          <el-input
            size="small"
            v-model="formData.tags"
            placeholder=""
          ></el-input>
        </el-form-item>
        <el-form-item prop="remarks" label="备注" style="font-weight: 700">
          <el-input
            size="small"
            v-model="formData.remarks"
            placeholder=""
          ></el-input>
        </el-form-item>
      </el-form>
      <el-row type="flex" justify="end">
        <!-- 取消提交按钮 -->
        <el-button @click="showDialog = false" size="small">取消</el-button>
        <!-- 提交按钮 -->
        <el-button @click="btnOk" type="primary" size="small">确定</el-button>
      </el-row>
    </el-dialog>
  </el-card>
  <!-- </div> -->
</template>
<script>
import {
  getQiyeListApi,
  getQiyeInfoApi,
  addQiyeApi,
  editQiyeApi,
  delQiyeInfoApi,
  editQiyeStateApi,
} from "@/api/Qiye";
import { provinces, citys } from "@/api/citys";
export default {
  name: "QiYe",
  data() {
    return {
      provincesList: provinces(), // 城市
      citysList: [], // 地区
      total: 0,
      list: [],
      showDialog: false,
      queryData: {
        page: 1,
        pagesize: 10,
        tags: "",
        province: "",
        city: "",
        shortName: "",
      },
      formData: {
        isFamous: true,
        shortName: "",
        company: "",
        province: "",
        city: "",
        tags: "",
        remarks: "",
      },
      formRules: {
        shortName: [{ required: true, message: "请输入", trigger: "blur" }],
        company: [{ required: true, message: "请输入", trigger: "blur" }],
        province: [{ required: true, message: "请选择", trigger: "change" }],
        city: [{ required: true, message: "请选择", trigger: "change" }],
        tags: [{ required: true, message: "请输入", trigger: "blur" }],
        remarks: [{ required: true, message: "请输入", trigger: "blur" }],
      },
    };
  },
  created() {
    this.initData();
  },
  methods: {
    // 选择城市的方法
    changeFn() {
      // 获取地区赋值给本地
      this.citysList = citys(this.queryData.province);
    },
    changeCn() {
      // 获取地区赋值给本地
      this.citysList = citys(this.formData.province);
    },
    // 企业管理列表数据渲染
    async initData() {
      let res = await getQiyeListApi(this.queryData);
      console.log(res);
      this.total = res.counts;
      this.list = res.items;
    },
    // 页码变化事件
    handleCurrentChange(val) {
      this.queryData.page = val;
      this.initData();
    },
    // 每页数量变化事件
    handleSizeChange(val) {
      // val 最新每页数量
      this.queryData.pagesize = val;
      this.initData();
    },
    // 清空搜索框
    resetSearch() {
      this.queryData = {};
      this.state = "";
      this.initData();
    },
    // 修改当前行数据
    async editHandler(val) {
      // val是当前点击行的数据
      // 发送查询当前点击行数据的请求
      let res = await getQiyeInfoApi(val.id);
      console.log(res);
      // 赋值给表单
      this.formData = res;
      if (res.isFamous === 1) {
        this.formData.isFamous = true;
      } else {
        this.formData.isFamous = false;
      }
      console.log(this.formData.isFamous);
      // 弹框打开
      this.showDialog = true;
    },
    // 事件提交
    async btnOk() {
      // 表单校验
      try {
        await this.$refs.form.validate();
      } catch (error) {
        return console.log(error);
      }
      // 发送请求，判断不同的逻辑
      if (this.formData.id) {
        // 有当前点击行id，则为修改功能
        await editQiyeApi(this.formData);
      } else {
        // 无当前点击行id，即为新增功能
        await addQiyeApi(this.formData);
      }
      // 提示成功
      this.$message.success("操作成功");
      // 关闭弹框
      this.showDialog = false;
      // 更新列表
      this.initData();
    },
    // 弹框关闭后重置表单
    close() {
      this.formData = {};
    },
    // 删除事件
    async delHandler(id) {
      // 弹出确认框
      try {
        await this.$confirm("是否确认删除？", "提示", {
          type: "warning",
        });
      } catch (error) {
        return this.$message("你取消了删除操作");
      }
      // 发送删除请求，传递删除id
      await delQiyeInfoApi(id);
      // 提示成功
      this.$message.success("删除成功");
      if (this.queryData.page !== 1 && this.list.length === 1) {
        this.queryData.page--;
      }
      // 更新列表
      this.initData();
    },
    // 改变启用/禁用状态的事件
    async editState(val) {
      if (val.state * 1 === 1) {
        try {
          await this.$confirm("已成功禁用，是否继续？", "提示", {
            type: "warning",
          });
        } catch (error) {
          return this.$message("你取消了该操作");
        }
      } else {
        try {
          await this.$confirm("已成功启用，是否继续？", "提示", {
            type: "warning",
          });
        } catch (error) {
          return this.$message("你取消了该操作");
        }
      }
      const state = val.state * 1 === 1 ? 0 : 1;
      await editQiyeStateApi({
        id: val.id,
        state,
      });
      this.$message.success("操作成功");
      this.initData();
    },
  },
};
</script>
<style lang="less" scoped>
.el-dialog__header {
  background-color: #409eff !important;
  .el-dialog__title {
    color: #fff;
  }
  .el-dialog__close {
    color: #fff !important;
  }
}
</style>
